
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  mail;

import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import logger.PoolLogger;
import dbmanager.DBManager;

public class SndMail
{
	//private static final String SMTP_HOST_NAME = "mail.osc.co.in";
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
	Vector<Object> to = new Vector<Object>();
	PoolLogger pl;
	
	public SndMail() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public boolean postMail(String from, Vector<Object> toAdd,String fileAttachment,String subject,String bodyText) 
	{
		try
		{
			
			to.addAll(toAdd);
			 //String host = "osc.co.in";
			 //String from = "mahendra@osc.co.in";
			 //String to = "maruteeg@osc.co.in";
			 //String fileAttachment = "c://1.txt";
			 Properties props = System.getProperties();

			 props.put("mail.smtp.host", SMTP_HOST_NAME);
			 props.put("mail.Transport.protocol","smtp");
			 props.put("mail.smtp.starttls.enable", "true");
			 //props.put("mail.smtp.port","25");
			 props.put("mail.smtp.port","587");
			 props.put("mail.smtp.auth","true");
//			 Setup mail server
			    Authenticator auth = new PopupAuthenticator();
			    Session session =  Session.getInstance(props, auth);
			 try
			 {
				MimeMessage message =    new MimeMessage(session);
			    message.setFrom( new InternetAddress(from));
			    for(int i=0;i<to.size();i++)
				{
			    	
			    	String add = null;
					try
					{
						add = to.get(i).toString();
						//System.out.println("Address :"+add);
						message.addRecipient(Message.RecipientType.TO,new InternetAddress(add));
					}
					catch (Exception e)
					{
						e.printStackTrace();
						System.out.println(add+" Adress not found");
						pl.info(add+" Adress not found");
						pl.debug(add+" Adress not found");
						pl.error(add+" Adress not found");
						pl.trace(add+" Adress not found");
						pl.fatal(add+" Adress not found");
						pl.warn(add+" Adress not found");
					}
				}
			    Address[]mim = message.getRecipients(Message.RecipientType.TO);
			    for(int i=0;i<mim.length;i++)
			    	System.out.println(mim[i].toString());
			    
			    message.setSubject(subject);
			    MimeBodyPart messageBodyPart =  new MimeBodyPart();
			    messageBodyPart.setText(bodyText);
			    Multipart multipart = new MimeMultipart();
			    multipart.addBodyPart(messageBodyPart);
			    messageBodyPart = new MimeBodyPart();
			    DataSource source = new FileDataSource(fileAttachment);
			    messageBodyPart.setDataHandler(new DataHandler(source));
			    messageBodyPart.setFileName(fileAttachment);
			    if(fileAttachment.length()!=0)
			    	multipart.addBodyPart(messageBodyPart);
			    message.setContent(multipart);
			   
			    Transport.send(message);
			    System.out.println("After Mail Send");
			    pl.info("After Mail Send");
				pl.debug("After Mail Send");
				pl.error("After Mail Send");
				pl.trace("After Mail Send");
				pl.fatal("After Mail Send");
				pl.warn("After Mail Send");
			    return true;
		    }
		    catch(Exception e)
		    {
		    	e.printStackTrace();
		    }
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	 }
	public static void main(String[] args) 
	{
		SndMail send = new SndMail();
		Vector<Object> vect = new Vector<Object>();
		vect.add("abhijit.salunkhe@wikiocean.net ");
		vect.add("marutee.gavalee@oscpro.in");
		vect.add("osradmin@gmail.com");
		//vect.add("fcmurray@gmail.com");
		send.postMail("bills@osc.co.in",vect , "//home//wiki//Desktop//desk doc//excise.ods", "Testing Mail", "Hi.. this is testing please ignore this message");
	}
	/*
	static class PopupAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("bills@osc.co.in", "billsosc");
        }
    }*/
	static class PopupAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
        	           return new PasswordAuthentication("osradmin@gmail.com", "wikiocean");
        }
    }

}
